package com.memorypalace.action;



import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.memorypalace.deck.biz.IDeckBiz;
import com.memorypalace.deck.biz.INoteBiz;
import com.memorypalace.deck.vo.DeckQueryPageBean;
import com.memorypalace.pojo.CardStyle;
import com.memorypalace.pojo.Deck;
import com.memorypalace.pojo.Note;
import com.memorypalace.pojo.User;
import com.memorypalace.util.AJaxUtil;

@Controller
@RequestMapping("/")
@SessionAttributes("bean")
public class DeckAction {
	@Autowired
	private IDeckBiz deckBiz;
	@Autowired
	private INoteBiz noteBiz;

	@RequestMapping("mydeck")
	public String mydeck() {
		return "/mydeck.html";
	}
	
	@RequestMapping("deckaction_mydeck")
	@ResponseBody
	public void todeck(DeckQueryPageBean bean,HttpServletRequest request,HttpServletResponse response) {
		// String id=bean.getUserId();

		User user = (User) request.getSession().getAttribute("user");
		System.out.println("用户信息:" + user);
		String userId = null;
		if(user != null){
			userId = user.getUserId();
		}
		bean.setUserId(userId);
		bean = deckBiz.getQueryPagedDataDecks(bean);
		
		 AJaxUtil.sendJsonByObj(bean, response);
	}
	
	@RequestMapping("deckaction_adddeck")
	public void addDeck(Deck deck, HttpServletResponse response,HttpServletRequest request) {
		deck.setDeckId(UUID.randomUUID().toString());
		//deck.setUserId("c34487b4-35e3-11e7-8e4c-e18a12b866d4");
		User user = (User) request.getSession().getAttribute("user");
		System.out.println("用户信息:" + user);
		String userId = null;
		if(user != null){
			userId = user.getUserId();
		}
		deck.setUserId(userId);
		System.out.println("=============================" + deck.getDeckName());
		Boolean addFlag = deckBiz.addDeck(deck);
		// //生成token,存放于 (1.session 2.页面隐藏域)
		// String addDeckToken = UUID.randomUUID()+"";
		// //给页面的
		// request.setAttribute("addDeckToken", addDeckToken);

		// request.getSession().setAttribute("addDeckToken", addDeckToken);
		AJaxUtil.sendJsonByObj(addFlag, response);
	}

	@RequestMapping("deckaction_modifydeck")
	public void modifyDeck(Deck deck, HttpServletResponse response) {
		Boolean modifyFlag = deckBiz.modifyDeck(deck);
		AJaxUtil.sendJsonByObj(modifyFlag, response);

	}

	@RequestMapping("deckaction_deletedeck")
	public void deleteDeck(String deckId, HttpServletResponse response) {
		Boolean deleteFlag = deckBiz.deleteDeck(deckId);

		AJaxUtil.sendJsonByObj(deleteFlag, response);

	}

	@RequestMapping("cardaction_getnotesbydeckId")
	@ResponseBody
	public void getNotesByDeckId(String deckId,HttpServletResponse response) {

		//cardstyle and note and noteType by deckId
		//List<NoteNoteTypeCardStyle> styleShow=deckBiz.getNoteNoteTypeCardStyle(deckId);
	    List<Note> noteList= noteBiz.getNotesByDeckId(deckId);
		AJaxUtil.sendJsonByObj(noteList, response);
	}

	// noteTypeId-->CardStyle
	@RequestMapping("cardaction_getcardstylebynotetypeId")
	public void getCardStyleByNotetyId(String noteTypeId, HttpServletResponse resopnse) {
		List<CardStyle> cardStyleList = deckBiz.getCardStyleByNotetyId(noteTypeId);
		
		AJaxUtil.sendJsonByObj(cardStyleList, resopnse);
		for (CardStyle cardStyle : cardStyleList) {
			System.out.println(cardStyle.getCardStyleName() + "?????????????????????????????????");
		}

	}

	@RequestMapping("/getdeckbydeckid")
	@ResponseBody
	public void getDeckByDeckId(String deckId,HttpServletResponse response){
		Deck deck = deckBiz.getDecksByDeckId(deckId);
		AJaxUtil.sendJsonByObj(deck, response);
	}
	
	
	
	// 最近三天的学习过的牌组
	@RequestMapping("/getrecentdeck")
	@ResponseBody
	public void getRecentDecksInThreeDays(HttpServletRequest request,HttpServletResponse response){
		String userId="c34487b4-35e3-11e7-8e4c-e18a12b866d4";
		User user = (User) request.getSession().getAttribute("user");
		userId = user.getUserId();
		Set<Deck> deckInThree=deckBiz.getRecentDecksByUserId(userId);
		AJaxUtil.sendJsonByObj(deckInThree, response);
	}
	
	

}
